<?php
/**
 * Created by PhpStorm.
 * User: eugene
 * Date: 2018/4/23
 * Time: 17:15
 */

namespace App\Transformers;


use App\Models\Member;
use Carbon\Carbon;

class GiftLogTransformer extends Transformer
{
    
    public function transform($item)
    {
        return [
            'id'=>$item->id,
            'from_user'=>(new MemberTransformer())->setTransformer('forSimple')->singleData(Member::find($item->user_id)),
            'gift_name'=>$item->gift->name,
            'diff_time'=>Carbon::parse($item->created_at)->diffForHumans(),
        ];
    }
    
    public function forMine($item)
    {
        return [
            'id'=>$item->id,
            'date'=>Carbon::parse($item->created_at)->toDateString(),
            'gift_name'=>$item->gift->name,
            'sended'=>(int)$item->is_sended,
            'category'=>(int)$item->category,
            'extra'=>starts_with($item->extra,'{')?json_decode($item->extra):null,
        ];
    }
    
}